package com.ifcar.app.data.dao.impl;

import com.ifcar.app.data.dao.PublishData;
import com.ifcar.app.db.DBUtil;
import com.ifcar.app.module.PublishMsg;
import com.ifcar.app.module.Question;
import com.ifcar.app.util.Dict;
import com.ifcar.app.util.StringUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;

/**
 * 信息发布数据层实现类
 * @author nxlixa
 *
 */
public class PublishDataImpl implements PublishData {
	
	private DB mDb;
	
	public PublishDataImpl(){
		mDb = DBUtil.mDb;
		if(null == mDb){
			// TODO ..
			System.err.println("db init error");
		}
		
	}
	
	// 发布求客信息
	@Override
	public WriteResult publishMsgFindPeople(PublishMsg pubObj) throws Exception{
		
		DBCollection pubMsgColl = mDb.getCollection(DBUtil.COLLECTION_PUBLISH_INFOR);
		DBObject dbObj = new BasicDBObject();

		if(!StringUtil.isEmptyStr(pubObj.isOnwer)){
			dbObj.put(Dict.ISONWER, pubObj.isOnwer);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.account)){
			dbObj.put(Dict.PHONENUM, pubObj.account);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.location)){
			dbObj.put(Dict.LOCATIOIN, pubObj.location);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.begin)){
			dbObj.put(Dict.SLOCATIOIN, pubObj.begin);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.end)){
			dbObj.put(Dict.ELOCATIOIN, pubObj.end);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.center)){
			dbObj.put(Dict.CLOCATIOIN, pubObj.center);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.iscircle)){
			dbObj.put(Dict.ISCIRCLE, pubObj.iscircle);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.pnumber)){
			dbObj.put(Dict.PNUMBER, pubObj.pnumber);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.price)){
			dbObj.put(Dict.PRICE, pubObj.price);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.stime)){
			dbObj.put(Dict.STIME, pubObj.stime);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.rtime)){
			dbObj.put(Dict.RTIME, pubObj.rtime);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.cartype)){
			dbObj.put(Dict.CARTYPE, pubObj.cartype);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.carcode)){
			dbObj.put(Dict.CARCODE, pubObj.carcode);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.digest)){
			dbObj.put(Dict.DIGEST, pubObj.digest);
		}
		
		WriteResult wr = pubMsgColl.save(dbObj);
		
		return wr;
	}
	
	// 发布求客信息
	@Override
	public WriteResult publishMsgFindCar(PublishMsg pubObj) throws Exception {
		
		DBCollection pubMsgColl = mDb.getCollection(DBUtil.COLLECTION_PUBLISH_INFOR);
		DBObject dbObj = new BasicDBObject();

		if(!StringUtil.isEmptyStr(pubObj.isOnwer)){
			dbObj.put(Dict.ISONWER, pubObj.isOnwer);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.account)){
			dbObj.put(Dict.PHONENUM, pubObj.account);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.location)){
			dbObj.put(Dict.LOCATIOIN, pubObj.location);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.begin)){
			dbObj.put(Dict.SLOCATIOIN, pubObj.begin);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.end)){
			dbObj.put(Dict.ELOCATIOIN, pubObj.end);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.center)){
			dbObj.put(Dict.CLOCATIOIN, pubObj.center);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.iscircle)){
			dbObj.put(Dict.ISCIRCLE, pubObj.iscircle);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.pnumber)){
			dbObj.put(Dict.PNUMBER, pubObj.pnumber);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.price)){
			dbObj.put(Dict.PRICE, pubObj.price);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.stime)){
			dbObj.put(Dict.STIME, pubObj.stime);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.rtime)){
			dbObj.put(Dict.RTIME, pubObj.rtime);
		}
		
		if(!StringUtil.isEmptyStr(pubObj.digest)){
			dbObj.put(Dict.DIGEST, pubObj.digest);
		}
		
		WriteResult wr = pubMsgColl.save(dbObj);
		
		return wr;
	}

	// 发布问题
	@Override
	public WriteResult publishQuestioin(Question qu) throws Exception {
		
		DBCollection pubMsgColl = mDb.getCollection(DBUtil.COLLECTION_SMARTHLEP_QUESTION);
		DBObject dbObj = new BasicDBObject();

		if(!StringUtil.isEmptyStr(qu.location)){
			dbObj.put(Dict.LOCATIOIN, qu.location);
		}

		if(!StringUtil.isEmptyStr(qu.locationName)){
			dbObj.put(Dict.LOCATIOINNAME, qu.locationName);
		}
		
		if(!StringUtil.isEmptyStr(qu.account)){
			dbObj.put(Dict.PHONENUM, qu.account);
		}
		
		if(!StringUtil.isEmptyStr(qu.questionContent)){
			dbObj.put(Dict.QUESTIONCONTENT, qu.questionContent);
		}
		
		WriteResult wr = pubMsgColl.save(dbObj);
		
		return wr;
	}

	// 回答问题
	@Override
	public WriteResult publishAnswer4Questioin(Question qu) throws Exception {
		
		DBCollection pubMsgColl = mDb.getCollection(DBUtil.COLLECTION_SMARTHLEP_ANSWER);
		DBObject dbObj = new BasicDBObject();

		if(!StringUtil.isEmptyStr(qu.location)){
			dbObj.put(Dict.LOCATIOIN, qu.location);
		}
		
		if(!StringUtil.isEmptyStr(qu.locationName)){
			dbObj.put(Dict.LOCATIOINNAME, qu.locationName);
		}
		
		if(!StringUtil.isEmptyStr(qu.account)){
			dbObj.put(Dict.PHONENUM, qu.account);
		}
		
		if(!StringUtil.isEmptyStr(qu.questionId)){
			dbObj.put(Dict.QUESTIONID, qu.questionId);
		}
		
		if(!StringUtil.isEmptyStr(qu.questionAnswer)){
			dbObj.put(Dict.QUESTIONANSWER, qu.questionAnswer);
		}
		
		WriteResult wr = pubMsgColl.save(dbObj);
		
		return wr;
	}
	
	/**
	 * 根据ID查找问题
	 */
	@Override
	public DBCursor findQuestionObjByQid(String questionId) throws Exception {
		
		DBCollection quColl = mDb.getCollection(DBUtil.COLLECTION_SMARTHLEP_QUESTION);
		
		DBObject obj = new BasicDBObject();
		obj.put(Dict.QUESTIONID, questionId);
		
		DBCursor cursor = quColl.find(obj);
		
		return cursor;
	}

}
